﻿using GraduationProject.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace GraduationProject.Webs.Areas.Admin.Controllers
{
    /// <summary>
    /// 基类控制器用于判断session用户登录
    /// </summary>
    public class AdminBaseController : Controller
    {
        /// <summary>
        /// 获取当前用户对象
        /// </summary>
        protected User CurrentUser
        {
            get
            {
                //从Session中获取用户对象
                if (SessionHelper.GetSession("adminloginUser") != null)
                {
                    return SessionHelper.GetSession("adminloginUser") as User;
                }

                return null;
            }
        }
        /// <summary>
        /// 登录验证用户Session是否过期
        /// </summary>
        /// <param name="filterContext"></param>
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            #region 登录用户验证
            if (this.CurrentUser == null)
            {
                if (filterContext.HttpContext.Request.IsAjaxRequest())
                {
                    filterContext.HttpContext.Response.AddHeader("SessionTimeout", "true");
                    filterContext.Result = new HttpStatusCodeResult(403, "您没有该权限，请登录！");
                    return;
                }
                else
                {
                    filterContext.HttpContext.Response.Write(
                   " <script type='text/javascript'> alert('由于您长时间未操作，系统已自动关闭。请重新登录');window.top.location='/Admin/AdminLogin/AdminLogin'; </script>");
                    filterContext.RequestContext.HttpContext.Response.End();
                    filterContext.Result = new EmptyResult();
                    return;
                }
            }
            base.OnActionExecuting(filterContext);
            #endregion
        }
    }
}